<?php

namespace Topxia\Service\Kuke\Dao\Impl;

use Topxia\Service\Common\BaseDao;
use Topxia\Service\Kuke\Dao\BaomingDao;
use Topxia\Common\DaoException;
use PDO;

class BaomingDaoImpl extends BaseDao implements BaomingDao
{
    protected $table = 'baoming';

    public function getBaomingList()
    {
        $sql = "SELECT * FROM {$this->table}";
		return $this->getConnection()->fetchAll($sql) ? : null;
    }
	
	public function checkShenfenzheng($sfz)
	{
        $sql = "SELECT shenfenzheng FROM {$this->table} where shenfenzheng='$sfz'";
		return $this->getConnection()->fetchColumn($sql) ? : null;
	}
	public function save($fields){
		// $sql = "insert into {$this->table} (xingming,zhunkaozheng,shenfenzheng,dianhua,zhuankeyuanxiao,zhuanye,benkeyuanxiaozhuanye,kaoshifenshu,zhuanyefenshu,banci,ganyan) values('".$fields['xingming']."','".$fields['zhunkaozheng']."','".$fields['shenfenzheng']."','".$fields['dianhua']."','".$fields['zhuankeyuanxiao']."','".$fields['zhuanye']."','".$fields['benkeyuanxiaozhuanye']."',".$fields['kaoshifenshu'].",".$fields['zhuanyefenshu'].",'".$fields['banci']."','".$fields['ganyan']."')";
		// return $sql;
		$fields['createdTime'] = time();
		$affected = $this->getConnection()->insert($this->table, $fields);
        if ($affected <= 0) {
            throw $this->createDaoException('Insert Baoming error.');
        }
        return true;
	}
	public function searchBaoming(array $conditions, array $orderBy, $start, $limit)
	{
		$this->filterStartLimit($start, $limit);
		$builder = $this->createBaomingQueryBuilder($conditions)
			->select('*')
            ->orderBy($orderBy[0], $orderBy[1])
			->setFirstResult($start)
			->setMaxResults($limit);
		return $builder->execute()->fetchAll() ? : array();
	}

	public function searchBaomingCount(array $conditions)
	{
		$builder = $this->createBaomingQueryBuilder($conditions)
            ->select('COUNT(shenfenzheng)');
        return $builder->execute()->fetchColumn(0);
	}
	
	

    protected function createBaomingQueryBuilder($conditions)
    {
        $conditions = array_filter($conditions,function($v){
            if($v === 0){
                return true;
            }
                
            if(empty($v)){
                return false;
            }
            return true;
        });
        if (isset($conditions['xingming'])) {
            $conditions['xingming'] = "%{$conditions['xingming']}%";
        }

        if (isset($conditions['zhunkaozheng'])) {
            $conditions['zhunkaozheng'] = "%{$conditions['zhunkaozheng']}%";
        }

        if (isset($conditions['shenfenzheng'])) {
            $conditions['shenfenzheng'] = "%{$conditions['shenfenzheng']}%";
        }

        if (isset($conditions['dianhua'])) {
            $conditions['dianhua'] = "%{$conditions['dianhua']}%";
        }

        if (isset($conditions['zhuankeyuanxiao'])) {
            $conditions['zhuankeyuanxiao'] = "%{$conditions['zhuankeyuanxiao']}%";
        }

        if (isset($conditions['zhuanye'])) {
            $conditions['zhuanye'] = "%{$conditions['zhuanye']}%";
        }

        if (isset($conditions['benkeyuanxiaozhuanye'])) {
            $conditions['benkeyuanxiaozhuanye'] = "%{$conditions['benkeyuanxiaozhuanye']}%";
        }

        if (isset($conditions['kaoshifenshu'])) {
            $conditions['kaoshifenshu'] = "{$conditions['kaoshifenshu']}";
        }

        if (isset($conditions['zhuanyefenshu'])) {
            $conditions['zhuanyefenshu'] = "{$conditions['zhuanyefenshu']}";
        }

        if (isset($conditions['banci'])) {
            $conditions['banci'] = "%{$conditions['banci']}%";
        }

        if (isset($conditions['ganyan'])) {
            $conditions['ganyan'] = "%{$conditions['ganyan']}%";
        }
        $builder = $this->createDynamicQueryBuilder($conditions)
            ->from($this->table, 'baoming')
            ->andWhere('xingming LIKE :xingming')
            ->andWhere('zhunkaozheng LIKE :zhunkaozheng')
            ->andWhere('shenfenzheng LIKE :shenfenzheng')
            ->andWhere('dianhua LIKE :dianhua')
            ->andWhere('zhuankeyuanxiao LIKE :zhuankeyuanxiao')
            ->andWhere('zhuanye LIKE :zhuanye')
            ->andWhere('benkeyuanxiaozhuanye LIKE :benkeyuanxiaozhuanye')
            ->andWhere('kaoshifenshu = :kaoshifenshu')
            ->andWhere('zhuanyefenshu = :zhuanyefenshu')
            ->andWhere('banci LIKE :banci')
            ->andWhere('ganyan LIKE :ganyan');
        return $builder;
    }
}